package com.elm.controller;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.elm.entity.Food;
import com.elm.service.FoodService;
import com.elm.service.impl.FoodServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class FoodController implements BaseController{
    private final FoodService foodService;
    public FoodController() {
        foodService = new FoodServiceImpl();
    }

    @Override
    public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String action = req.getPathInfo().split("/")[2];

        switch (action) {
            case "listFoodByBusinessId":
                getListFoodByBusinessId(req, resp);
                break;
        }
    }


    /**
     * 根据id查询菜品信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void getListFoodByBusinessId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        String businessId = req.getParameter("businessId");
        System.out.println("根据店铺id查询菜品信息" + businessId);

        List<Food> foods = foodService.getFoodsById(businessId);
        resp.setContentType("application/json;charset=UTF-8");

        if (foods == null) {
            resp.getWriter().write("{\"error\":\"查询不到菜品信息\"}");
        }else {
            resp.getWriter().write(new Gson().toJson(foods));
        }
    }


}
